package dmt16.TelemetrySuit.UI.Tiles
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.MMGSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Assets.MMGTileGFX;
	import dmt16.TelemetrySuit.UI.Colours;
	import dmt16.TelemetrySuit.UI.PlotTools.Graph;
	import dmt16.TelemetrySuit.UI.PlotTools.GraphLine;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class MMGTile extends TileBase
	{
		public function MMGTile(sensorLeft:MMGSensor,sensorRight:MMGSensor)
		{
			_tileGFX=new MMGTileGFX();
			tileContent=_tileGFX;
			
			_pointerLeft=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(1);
			_pointerRight=DisplayObjectContainer(_tileGFX.getChildAt(0)).getChildAt(2);
			
			_tileGFX.width=WIDTH;
			_tileGFX.height=HEIGHT;
			
			_txtLeft=new TextField();
			_txtLeft.embedFonts=true;
			_txtLeft.selectable=false;
			_txtLeft.defaultTextFormat=_textFormat;
			_txtLeft.x=0;
			_txtLeft.width=WIDTH*75/300;
			_txtLeft.y=HEIGHT*123/300;
			_txtLeft.height=HEIGHT*75/300;
			_txtLeft.text="L\r0%";
			addChild(_txtLeft);
			
			_txtRight=new TextField();
			_txtRight.embedFonts=true;
			_txtRight.selectable=false;
			_txtRight.defaultTextFormat=_textFormat;
			_txtRight.x=0;
			_txtRight.width=WIDTH*75/300;
			_txtRight.y=HEIGHT*216/300;
			_txtRight.height=HEIGHT*75/300;
			_txtRight.text="R\r0%";
			addChild(_txtRight);
			
			var titleText:TextField=new TextField();
			titleText.embedFonts=true;
			titleText.selectable=false;
			titleText.defaultTextFormat=_textFormat2;
			titleText.x=0;
			titleText.y=10*HEIGHT/300;
			titleText.width=WIDTH;
			titleText.height=155*HEIGHT/300;
			titleText.text="muscle\ractivity";
			addChild(titleText);
			
			_detailView=new Graph();
			detailContent=_detailView;
			
			_sensorLeft=sensorLeft;
			_sensorRight=sensorRight;
			
			if(_sensorLeft&&_sensorRight){
				_sensorLeft.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onLeftData);
				_sensorRight.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onRightData);
				
				_dataLineLeft=new GraphLine(_sensorLeft.data);
				_dataLineLeft.lineStyle.color=Colours.C1;
				
				_dataLineRight=new GraphLine(_sensorRight.data);
				_dataLineRight.lineStyle.color=Colours.C2;
				
				_detailView.addLine(_dataLineLeft);
			}
		}
		
		private var _detailView:Graph;
		private var _dataLineLeft:GraphLine;
		private var _dataLineRight:GraphLine;
		
		private var _pointerLeft:DisplayObject;
		private var _pointerRight:DisplayObject;
		
		private var _tileGFX:MMGTileGFX;
		private var _txtLeft:TextField;
		private var _txtRight:TextField;
		
		private var _sensorLeft:MMGSensor;
		private var _sensorRight:MMGSensor;
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=34*HEIGHT/300;
			_textFormat.align=TextFormatAlign.CENTER;
			_textFormat.color=Colours.FG;
			_textFormat.leading=-6;
		}
		private static var _textFormat2:TextFormat;
		{
			_textFormat2=new TextFormat();
			_textFormat2.font=Fonts.DSDigitalBold;
			_textFormat2.size=45*HEIGHT/300;
			_textFormat2.align=TextFormatAlign.CENTER;
			_textFormat2.color=Colours.FG;
			_textFormat.leading=-8;
		}
		
		private function _onLeftData(event:SensorEvent):void{
			var p:Number=_sensorLeft.percentage;
			_pointerLeft.x=(p*90+50)*HEIGHT/300;
			_txtLeft.text="L\r"+Math.round(100*p)+"%";
		}
		
		private function _onRightData(event:SensorEvent):void{
			var p:Number=_sensorRight.percentage;
			_pointerRight.x=(p*90+50)*HEIGHT/300;
			_txtRight.text="R\r"+Math.round(100*p)+"%";
		}
		
		public function setOut(l:Number,r:Number):void{
			_pointerLeft.x=(l*90+50)*HEIGHT/300;
			_txtLeft.text="L\r"+Math.round(100*l)+"%";
			_pointerRight.x=(r*90+50)*HEIGHT/300;
			_txtRight.text="R\r"+Math.round(100*r)+"%";
		}
		
		public override function drawDetailContent():void{
			_detailView.fullRedraw();
		}
	}
}